<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event;
class PermissionController extends Controller
{
    protected $fields = [
        'rules' => '',
        'name' => '',
        'cid' => 0,
        'icon'=>'',
        'is_show'=>'',
    ];

    public function index(Request $request, $cid = 0)
    {
        $res = DB::table('permissions')->where('cid',$cid)->orderby('paixu')->orderby('id')->paginate(10);
        $count = DB::table('permissions')->where('cid',$cid)->count();
        return view('admin.permission.index', ['res' => $res,'cid'=>$cid,'count'=>$count]);
    }

    public function add($cid,Request $request)
    {
        if($request->isMethod('post')){
            if(Input::get('is_show') == 1 && Input::get('rules') == '' && $cid != 0){
                echo "<script>alert('url不能为空!');window.history.go(-1);</script>";
                exit();
            }
            $data = Input::except('_token');
            $data['cid'] = $cid;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $app = Input::get('app') ? Input::get('app').'\\': '';
            if(Input::has('controller') && Input::has('action')){
                $data['path'] = 'App\Http\Controllers\\'.$app.Input::get('controller').'Controller@'.Input::get('action');
            }
            DB::table('permissions')->insert($data);
            return redirect('admin/permission/index/'.$cid);
        }
        return view('admin.permission.add');
    }

    public function edit($id,Request $request)
    {
        $re = DB::table('permissions')->where('id',$id)->first();
        if($request->isMethod('post')){
            $obj = DB::table('permissions');
            $cid = $re->cid;
            if(Input::get('is_show') == 1 && Input::get('rules') == '' && $cid != 0){
                echo "<script>alert('url不能为空!');window.history.go(-1);</script>";
                exit();
            }
            $data = Input::except('_token');
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $app = Input::get('app') ? Input::get('app').'\\': '';
            if(Input::has('controller') && Input::has('action')){
                $data['path'] = 'App\Http\Controllers\\'.$app.Input::get('controller').'Controller@'.Input::get('action');
            }
            $obj->where('id',$id)->update($data);
            $cid = $obj->where('id',$id)->value('cid');
            return redirect('admin/permission/index/'.$cid);
        }
        return view('admin.permission.edit', ['re' => $re]);
    }

    public function del($id)
    {
        $child = db::table('permissions')->where('cid', $id)->first();
        if ($child) {
            echo "<script>alert('该权限下有子权限!');window.history.go(-1);</script>";
            exit();
        }else{
            db::table('permissions')->where('id',$id)->delete();
            return redirect()->back();
        }
    }

    //排序
    public function save_sort(){
        $where['id'] = $_POST['id'];
        $data['paixu'] = $_POST['paixu'];
        db::table('permissions')->where($where)->update($data);
    }
}
